/************************************************************************/
/* GUI                                                                  */
/************************************************************************/
#include "GUI/Panels/WaveInfoPanel.h"

namespace TD_GUI {


	/************************************************************************/
	/* Default definitions                                                  */
	/************************************************************************/
	const string WaveInfoPanel::WaveInfoPanelBackground = "GUI/Panels/creepInfo.png";
	const Coord2D WaveInfoPanel::WaveInfoPanelPosition(0.1005*screenWidth, 0.8437*screenHeight);
	const Coord2D WaveInfoPanel::WaveInfoPanelSize(0.18622*screenWidth, 0.4375*screenHeight);
	const string WaveInfoPanel::waveBaseName = "GUI/WaveInfo/wave";
	const unsigned int WaveInfoPanel::numberOfWaves = 53;
	/*const float WaveInfoPanel::barVelocity = 0.25/60.0;*/
	const float WaveInfoPanel::barVelocity = 0.35/60.0;


	string WaveInfoPanel::waveName(unsigned int waveIndex) {
		stringstream wName;
		wName << waveBaseName << waveIndex << ".png";
		return (wName.str());
	}

	void WaveInfoPanel::update(unsigned int deltaTime) {
		Coord2D panelInc(167 * screenWidth/1024.0, 100 * screenHeight/768.0);

		// Calculates the correct displacement 
		m_displacement += deltaTime * barVelocity;
		if(m_displacement >= 1) {
			if(m_wavesTiles.size() > 0) {
				m_wavesTiles.front()->getClipping()->x += 1; 
			}
			if(m_wavesTiles.size() > 0 && m_wavesTiles.front()->getClipping()->x >= panelInc.x + 1) { 
				//If left clipping margin from the first element has exceeded the image width
				//Removes and deletes the first tile and clipping rectangle in the deque
				Tile *pTile = m_wavesTiles.front();
				SDL_Rect *pRect = m_wavesClips.front();
				delete pTile;
				delete pRect;
				if(m_wavesTiles.size() > 0) {
					m_wavesTiles.pop_front();
					m_wavesClips.pop_front();
					
					if(m_wavesTiles.size() > 0) {
						//Resets the bar position
						m_position = WaveInfoPanelPosition;
						//The first tile in the deque has to be updated
						m_wavesTiles.front()->setPosition(m_position);
					}
				}
				
				
				
				//The last showing element in the deque has also to be
				//updated
				if(m_wavesTiles.size() > 4) {
					m_wavesTiles[4]->getClipping()->w = 0;
				}
				m_currWave++;
			} else { 
				//The left clipping margin from the first element didn't exceed the image width
				m_position.x -= 1;
			}
			if(m_wavesTiles.size() > 4) {
				m_wavesTiles[4]->getClipping()->w += 1; 
			}

			m_displacement = 0;
		}

		//Iterates through all the tiles and update the positions
		for(unsigned int i = 1; i < m_wavesTiles.size(); i++) {
			m_wavesTiles[i]->setPosition((m_position + Coord2D((i) * panelInc.x, 0)));
		}


	}

	WaveInfoPanel::WaveInfoPanel() : BasePanel(WaveInfoPanelPosition, WaveInfoPanelSize) {

		/*m_pImage = ResourceManager::loadImage(m_creepInfoBackground);*/

		Coord2D panelInc(167 * screenWidth/1024.0, 100 * screenHeight/768.0);
		
		for(unsigned int i = 1; i <= numberOfWaves; i++) {
			m_wavesClips.push_back(new SDL_Rect());
			m_wavesClips.back()->x = m_wavesClips.back()->y = 0;
			m_wavesClips.back()->w = panelInc.x; 
			m_wavesClips.back()->h = panelInc.y;
			m_wavesTiles.push_back(new Tile(m_position + Coord2D((i-1) * panelInc.x, 0), panelInc, imgTile, m_wavesClips.back(), TD_Resources::ResourceManager::loadImage(waveName(i))));
		}
		
		m_wavesTiles[4]->getClipping()->w = 0; 

		for(unsigned int i = 0; i < m_wavesTiles.size(); i++) {
			m_wavesTiles[i]->getImage()->getSurface();
			//Setting the alpha channel, to produce transparency
			SDL_SetAlpha(m_wavesTiles[i]->getImage()->getSurface(), SDL_SRCALPHA | SDL_RLEACCEL, 200);
			
		}
		m_isRolling = false;
		m_displacement = 0;
		m_currWave = 1;

	}

	WaveInfoPanel::WaveInfoPanel(const WaveInfoPanel &rhs) {

	}

	WaveInfoPanel & WaveInfoPanel::operator =(const WaveInfoPanel & rhs) {
		return(*this);
	}

	WaveInfoPanel::~WaveInfoPanel() {
		deque<Tile *>::iterator it;
		for(it = m_wavesTiles.begin(); it != m_wavesTiles.end(); it++) { //review iterator
			/*Utils::safeDelete<Tile>(it.value);*/
		}
		
		deque<SDL_Rect *>::iterator it2;
		for(it2 = m_wavesClips.begin(); it2 != m_wavesClips.end(); it2++) {
			/*Utils::safeDelete(SDL_Rect)(it.value);*/
		}
		

		
	}

	/************************************************************************/
	/* Rendering                                                            */
	/************************************************************************/
	
	void WaveInfoPanel::startNextWave() {
		Coord2D panelInc(167 * screenWidth/1024.0, 100 * screenHeight/768.0);

		if(m_wavesTiles.size() > 0) {
			Tile *pTile = m_wavesTiles.front();
			SDL_Rect *pRect = m_wavesClips.front();
			delete pTile;
			delete pRect;
			m_wavesTiles.pop_front();
			m_wavesClips.pop_front();

			if(m_wavesTiles.size() > 0) {
				//Resets the bar position
				m_position = WaveInfoPanelPosition;
				//The first tile in the deque has to be updated
				m_wavesTiles.front()->setPosition(m_position);
			}

			
			m_currWave++;
			//The last showing element in the deque has also to be
			//updated
			
			if(m_wavesTiles.size() > 4) {
				m_wavesTiles[3]->getClipping()->w = panelInc.x;
				m_wavesTiles[4]->getClipping()->w = 0;
			}
		}
		
		

		
	}
	void WaveInfoPanel::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		BasePanel::render(g_destSurface, deltaTime);
		if(m_isRolling) {
			update(deltaTime);
		}
		if(m_wavesTiles.size() >= 5) {
			for(unsigned int i = 0; i < 5; i++) {
				m_wavesTiles[i]->render(g_destSurface, deltaTime);
			}
		} else {
			for(unsigned int i = 0; i < m_wavesTiles.size() ; i++) {
				m_wavesTiles[i]->render(g_destSurface, deltaTime);
			}
		}
		
		
	}

	interaction_t WaveInfoPanel::processEvent(const SDL_Event &event) {
		interaction_t interaction;
		interaction.type = noInteraction;
		return(interaction);
	}

	void WaveInfoPanel::restart() {
		Coord2D panelInc(167 * screenWidth/1024.0, 100 * screenHeight/768.0);


		while(m_wavesTiles.size() > 0) {
			Tile *pTile = m_wavesTiles.front();
			SDL_Rect *pRect = m_wavesClips.front();
			delete pTile;
			delete pRect;
			m_wavesTiles.pop_front();
			m_wavesClips.pop_front();
		}
		
		for(unsigned int i = 1; i <= numberOfWaves; i++) {
			m_wavesClips.push_back(new SDL_Rect());
			m_wavesClips.back()->x = m_wavesClips.back()->y = 0;
			m_wavesClips.back()->w = panelInc.x;
			m_wavesClips.back()->h = panelInc.y;
			m_wavesTiles.push_back(new Tile(m_position + Coord2D((i-1) * panelInc.x, 0), panelInc, imgTile, m_wavesClips.back(), TD_Resources::ResourceManager::loadImage(waveName(i))));
		}

		m_wavesTiles[4]->getClipping()->w = 0; 

		for(unsigned int i = 0; i < m_wavesTiles.size(); i++) {
			m_wavesTiles[i]->getImage()->getSurface();
			//Setting the alpha channel, to produce transparency
			SDL_SetAlpha(m_wavesTiles[i]->getImage()->getSurface(), SDL_SRCALPHA | SDL_RLEACCEL, 200);

		}

		m_isRolling = false;
		m_displacement = 0;
		m_currWave = 1;
	}
}